package com.longling.hayarider.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import javax.net.ssl.HttpsURLConnection;

import okhttp3.Request;

/**
 * @ClassName Common
 * @Description TODO 常用工具类
 * @date 2021-07-07 18:22
 * @Company LongLing
 * @Version 1.0
 * @Author 潇妙
 */
public class Common {

    private final String  TAG =this.getClass().getName();
    /**
     * api接口
     */
    private RequrestUtil requrestUtil;
    /**
     * 网络图片转换bitmap格式的图片
     * @param url
     * @return
     */
    public Bitmap getBitmap(String url){
        Bitmap bitmap = null;
        try {
            URL iconurl = new URL(url);
            URLConnection urlConnection = iconurl.openConnection();
            HttpsURLConnection httpURLConnection = (HttpsURLConnection) urlConnection;
            int length = httpURLConnection.getContentLength();
            urlConnection.connect();
            InputStream inputStream =urlConnection.getInputStream();
            BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream,length);
            bitmap = BitmapFactory.decodeStream(bufferedInputStream);
            bufferedInputStream.close();
            inputStream.close();
        }catch (IOException e){
            Log.e(TAG,e.getMessage());
        }
        return  bitmap;
    }

    /**
     * 获取meta中的值
     * @param context
     * @param metaKey
     * @return
     */
    public String getMetaValue(Context context,String metaKey){
            Bundle metaData = null;
            String apiKey = null;
            if (context == null || metaKey == null) {
                return null;
            }
            try {
                ApplicationInfo ai = context.getPackageManager()
                        .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
                if (null != ai) {
                    metaData = ai.metaData;
                }
                if (null != metaData) {
                    apiKey = metaData.getString(metaKey);
                }
            } catch (PackageManager.NameNotFoundException e) {
            }
            return apiKey;
    }

    /**
     * 设置meta的属性
     * @param context
     * @param key
     * @param value
     */
    public void setMetaValue(Context context,String key,String value){
        ApplicationInfo appi;
        try {
            appi = context.getPackageManager().getApplicationInfo(
                    context.getPackageName(), PackageManager.GET_META_DATA);
            appi.metaData.putString(key, value);
        } catch (PackageManager.NameNotFoundException e1) {
            e1.printStackTrace();
        }
    }



}
